//
//  hDisplayView.m
//  练习
//
//  Created by Hero11223 on 16/5/19.
//  Copyright © 2016年 zyy. All rights reserved.
//

#define MainScreen_width  [UIScreen mainScreen].bounds.size.width//宽
#define MainScreen_height [UIScreen mainScreen].bounds.size.height//高

#import "hDisplayView.h"
#import "UIImageView+WebCache.h"
#import "GCD.h"
#import "CYHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "AFNetworking.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "NSObject+XML.h"
#import "CYManceXml.h"



@interface hDisplayView ()<UIScrollViewDelegate>
{
    UIScrollView    *_bigScrollView;
    NSMutableArray  *_imageArray;
    UIPageControl   *_pageControl;
}
@property(nonatomic, strong) GCDTimer *GCDTimer;

@end

@implementation hDisplayView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         
     

         
         [CYHttpTool getSerializerWithURL:@"http://image.credit-manage.com/app/maintenance.xml" params:nil success:^(id json) {
              
              CYManceXml *manceXml = [CYManceXml objectWithXMLDataname:json];
              if ([manceXml.hasMaintenance isEqualToString:@"true"]) {
                   if (manceXml.message) {
                        [[[UIAlertView alloc]initWithTitle:@"提示" message:manceXml.message delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil] show];
                        return ;
                   }else{
                          [[[UIAlertView alloc]initWithTitle:@"提示" message:@"系统正在维护中，请稍后重试" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil] show];
                        return;
                   }
                 
              }else{
                  //do nothing;
              }
              
         } failure:^(NSError *error) {
              
         }];

         
        _imageArray = [@[@"闪屏1.png",@"闪屏2.png"]mutableCopy];
//         _imageArray = [[NSMutableArray alloc]mutableCopy];
//        _imageArray = [NSMutableArray arrayWithObjects:@"闪屏1.png",@"闪屏2.png", @"闪屏3.png",@"闪屏4.png", nil];
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)];
        
        scrollView.contentSize = CGSizeMake((_imageArray.count + 1)*MainScreen_width, MainScreen_height);
        //设置反野效果，不允许反弹，不显示水平滑动条，设置代理为自己
        scrollView.pagingEnabled = YES;//设置分页
        scrollView.bounces = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        _bigScrollView = scrollView;
         
         dispatch_async(dispatch_get_main_queue(), ^{
              [CYHttpTool postWithURL:[CYURL shared].getAdPic params:nil success:^(id json) {
                   CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
                   if (jsoninfo.isSuccess) {
                        _imageArray = jsoninfo.data;
                        for (int i = 0; i < _imageArray.count; i++) {
                             UIImageView *imageView = [[UIImageView alloc]init];
                             imageView.frame = CGRectMake(i * MainScreen_width, 0, MainScreen_width, MainScreen_height);
                             UIImage *image = [UIImage imageNamed:_imageArray[i]];
                             imageView.image = image;
                             [imageView sd_setImageWithURL:[NSURL URLWithString:_imageArray[i]]];
                             [scrollView addSubview:imageView];
                             
                             UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_bigScrollView.width-60, 30, 50, 20)];
                             [button setTitle:@"跳过" forState:UIControlStateNormal];
                             [button setBackgroundColor:[UIColor clearColor]];
                             [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
                             [self addSubview:button];

                             
                             UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(MainScreen_width/2, MainScreen_height - 60, 0, 40)];
                             pageControl.numberOfPages = _imageArray.count;
                             pageControl.backgroundColor = [UIColor clearColor];
                             [self addSubview:pageControl];
                             _pageControl = pageControl;
                             
                        }
                        
                        //添加手势
                        UITapGestureRecognizer *singleRecognizer;
                        singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
                        singleRecognizer.numberOfTapsRequired = 1;
                        [scrollView addGestureRecognizer:singleRecognizer];
                        
                        
                   }
                   
                   
              } failure:^(NSError *error) {
                   
                   
              }];

              
         });
         

         
               
         
    }
     [self countdown];
     
    return self;
}
-(void)close{
     self.hidden = YES;
}

/**
 *  倒计时
 */

- (void)countdown{
     self.GCDTimer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
     
     CYWeakObject(weakself, self);
     __block int time = 5;
     [self.GCDTimer event:^{
          if (time < 1) {
               //设置界面的按钮显示 根据自己需求设置
               self.hidden = YES;
               //            btn.backgroundColor = CYColor(79, 118, 245, 1);
               [weakself.GCDTimer destroy];
          } else {
               //        NSLog(@"time:%d", time);
               //            btn.backgroundColor = CYColor(211, 211, 211, 1);
          }
          time--;
     } timeInterval:NSEC_PER_SEC * 1];
     [self.GCDTimer start];
}


-(void)handleSingleTapFrom
{
    if (_pageControl.currentPage == 3) {
        self.hidden = YES;
        
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _bigScrollView) {
        
        CGPoint offSet = scrollView.contentOffset;
        
        _pageControl.currentPage = offSet.x/(self.bounds.size.width);//计算当前的页码
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_pageControl.currentPage), scrollView.contentOffset.y) animated:YES];
        
    }
    
    if (scrollView.contentOffset.x == (_imageArray.count) *MainScreen_width) {
        self.hidden = YES;
        
    }
    
}

@end
